package com.chen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.domain.SysMenu;
import com.chen.domain.SysPrivilege;
import com.chen.domain.SysRolePrivilege;
import com.chen.mapper.SysPrivilegeMapper;
import com.chen.mapper.SysRolePrivilegeMapper;
import com.chen.service.SysPrivilegeService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

@Service
public class SysPrivilegeServiceImpl extends ServiceImpl<SysPrivilegeMapper, SysPrivilege> implements SysPrivilegeService {


    @Resource
    private SysPrivilegeMapper sysPrivilegeMapper;

    @Resource
    private SysRolePrivilegeMapper sysRolePrivilegeMapper;

    @Override
    public List<SysPrivilege> findMenuPrivilege(Long menuId, Long roleId) {
        List<SysPrivilege> sysPrivilegesResult = new ArrayList<>();
        QueryWrapper<SysRolePrivilege> sysRolePrivilegeQueryWrapper = new QueryWrapper<>();
        QueryWrapper<SysRolePrivilege> rolePrivilege = sysRolePrivilegeQueryWrapper.eq("role_id", roleId);
        List<SysRolePrivilege> sysRolePrivileges = sysRolePrivilegeMapper.selectList(rolePrivilege);
        if (CollectionUtils.isEmpty(sysRolePrivileges)) {
            return Collections.emptyList();
        }
        QueryWrapper<SysPrivilege> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("menu_id", menuId);
        List<SysPrivilege> sysPrivileges = sysPrivilegeMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(sysPrivileges)) {
            return Collections.emptyList();
        }
        for (SysRolePrivilege sysRolePrivilege : sysRolePrivileges) {
            for (SysPrivilege sysPrivilege : sysPrivileges) {
                if (sysRolePrivilege.getPrivilegeId().equals(sysPrivilege.getId())) {
                    sysPrivilege.setOwn(1);
                }
            }
        }
        sysPrivilegesResult.addAll(sysPrivileges);
        return sysPrivilegesResult;
    }

    /**
     * 获取该菜单下面所有的权限
     *
     * @param menuId 菜单的ID
     * @param roleId roleId 代表当前的查询的角色的ID
     * @return
     */
    @Override
    public List<SysPrivilege> getAllSysPrivilege(Long menuId, Long roleId) {
        // 1 查询所有的该菜单下的权限
        List<SysPrivilege> sysPrivileges = list(new LambdaQueryWrapper<SysPrivilege>().eq(SysPrivilege::getMenuId, menuId));
        if (CollectionUtils.isEmpty(sysPrivileges)) {
            return Collections.emptyList();
        }
        // 2 当前传递的角色使用包含该权限信息也要放进去
        for (SysPrivilege sysPrivilege : sysPrivileges) {
            Set<Long> currentRoleSysPrivilegeIds = sysPrivilegeMapper.getPrivilegesByRoleId(roleId);
            if (currentRoleSysPrivilegeIds.contains(sysPrivilege.getId())) {
                sysPrivilege.setOwn(1); // 当前的角色是否有该权限
            }

        }
        return sysPrivileges;
    }

    @Override
    public void removeByRoleId(String roleId) {
        sysRolePrivilegeMapper.deleteByRoleId(roleId);
    }

    @Override
    public SysMenu findMenuById(String privilegeId) {
        return sysPrivilegeMapper.selectMenuById(privilegeId);
    }
}
